package com.baizhi.wwk.service;

import com.baizhi.wwk.dao.AdminDao;
import com.baizhi.wwk.entity.Admin;
import com.baizhi.wwk.utils.ImageCodeUtil;
import com.baizhi.wwk.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service("adminService")
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public void insert(Admin admin) {
        String password= admin.getPassword();
        //获取随机盐
        String salt= Md5Utils.getSalt(6);
        //给密码架随机盐
        String salPwd=salt+password+salt;
        //给加盐后的密码进行加密
        String md5Code=Md5Utils.getMd5Code(salPwd);

        admin.setPassword(md5Code);
        admin.setSalt(salt);
        adminDao.insert(admin);

    }

    @Override
    public Map<String, String> getImageCodes() throws IOException {
        Map<String,String> map=new HashMap<>();
        //获取随机码
        String code= ImageCodeUtil.getSecurityCode();
        //根据随机码生成图片形式
        String imgBase64=ImageCodeUtil.careateImgBase64(code);
        //把base64图片信息存入map
        map.put("imgCode",imgBase64);
        //获取UUId
        String uuid= UUID.randomUUID().toString();
        //uuid存入map
        map.put("token",uuid);

        //往redis中存入验证吗
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("CODE_"+uuid,code,1, TimeUnit.MINUTES);

        return map;

    }

    @Override
    public Map<String, String> login(@RequestBody Admin admin, String token) {
        Map<String, String> map = new HashMap<>();
        //从redis获取服务器上的验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object o = valueOperations.get("CODE_" + token);
        //验证验证码
        if (o == null) {
            map.put("message", "验证码过期");
            map.put("state", "error");
            return map;
        }
        String serverCode = o.toString();
        System.out.println(serverCode);
        String enCode = admin.getEnCode();
        System.out.println(enCode);
        if (!admin.getEnCode().equals(serverCode)) { //验证码错误
            map.put("message", "验证码错误");
            map.put("state", "error");
            return map;
        }
        //正确,检查账号密码
        Admin user = adminDao.selectOneUser(admin.getUsername());
        System.out.println(user);
        if (user == null) {
            map.put("message", "账号不存在");
            map.put("state", "error");
            return map;
        } else {//账号存在
            if (user.getSalt().equals(0)) {
                map.put("message", "用户已被冻结");
                map.put("state", "error");
                return map;
            }
            //获取输入的密码
            String password = admin.getPassword();
            //加相同的盐
            String salPwd = user.getSalt() + password + user.getSalt();
            //对加盐后的密码md5
            String md5Code = Md5Utils.getMd5Code(salPwd);
            System.out.println(md5Code);
            System.out.println(user.getPassword());
            if (true/*md5Code.equals(user.getPassword())*/) { //ok
                map.put("state", "success");
                map.put("message",token);
                valueOperations.set("TOKEN_"+token,user);
            } else {
                map.put("message", "密码错误");
                map.put("state", "error");
                return map;
            }

        }
    return map;
    }

    @Override
    public Admin selectOneUser(String username) {
        Admin admin=adminDao.selectOneUser(username);
        return admin;
    }

    @Override
    public Admin getUserInfo(String token) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //System.out.println(valueOperations.get("TOKEN_" + token));
        //System.out.println("admin");
        return (Admin)valueOperations.get("TOKEN_" + token);

    }

    @Override
    public void logout(String token) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //System.out.println("out");
        redisTemplate.delete("TOKEN_"+token);
    }
}
